<?php
class orderapi{
    /**
     * 权限MD5加密
     */
    function SignUtil(){
        global $myconfigs;
        $agentid = $myconfigs['agentid'];
        $key = $myconfigs['key'];
        $sign = md5(md5($agentid.$key));
        $arr = array();
        $arr['agenid'] = $agentid;
        $arr['sign'] = $sign;
        return $arr;
    }
	
	//日志记录
    function setLogs($logs,$path="/data/Logs/orderapi/"){
        $fleName=$_SERVER['DOCUMENT_ROOT'].$path.date('Y-m-d',time())."_".date('H:i',time()).".txt";
        $th_time=fopen($fleName,"a");
        fwrite($th_time,date('Y-m-d H:i:s',time())."\r\n".$logs."\r\n\r\n\r\n");
        fclose($th_time);
    }
	
	function curl($action,$params=null,$url="http://open.qmango.com/hotel/"){
        if(!$action){return "服务名错误！";}
        global $myconfigs;
        $agentid = $myconfigs['agentid'];
        $key = $myconfigs['key'];
        
        $sign = $this->SignUtil();
        $get = $bookingString=$userAgent='';
        
        if($action == 'getorderprice'){
			$params['agentid']=$agentid;
			ksort($params);
			$psm="";
			foreach($params as $k=>$v){
				$psm.="&".$k."=".$v;
			}
			$psm=trim($psm,'&');
			$sign=md5($psm.$key);
			$curl=$url.$action."?".$psm."&sign=".$sign;
			//echo '<pre>';print_r($params);exit;
        }else{
            if($params){
                $get = '&';
                foreach($params as $key => $value){
                    $get .= $key.'='.$value."&";
                }
                $get = substr($get,0,strlen($get)-1);
            }
            $curl = $url.$action."?agentid=".$sign['agenid']."&sign=".$sign['sign'].$get;
        }
        
        //初始化
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $curl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if($action == 'booking'){
            //POST请求
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		}else{
            //GET请求
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt ( $ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8"));
        }
        $return = curl_exec ( $ch );
		$logs="调用地址：".$curl."\r\n接收数据：".$return."\r\n发送数据：".json_encode($params);
        $this->setLogs($logs);
        //释放curl句柄
        curl_close($ch);
		
		$data = simplexml_load_string($return);
        $xml = @json_decode(@json_encode($data),TRUE);
        
        if($xml){
			if($action="GetImmediateHotelList"){
				return $xml;
			}else{
				if(isset($xml['status'])&&$xml['status']==1){return $xml;}
				return "数据错误，请联系技术人员！";
			}
        }
        return "获取内容失败！";
    }
	
	
	//获取订单金额
    function getOrderPrice($hotelid=0,$roominfo=0,$roomNumber=0,$sdate="",$edate="",$price=0){
		$params=array('hotelid'=>$hotelid,'roominfo'=>$roominfo.'||'.$roomNumber,'sdate'=>$sdate,'edate'=>$edate);
		$re = $this->curl('getorderprice',$params);
        if($re&&is_array($re)&&!empty($re['price'])&&$price==$re['price']){
			return 1;
		}
		/*if($re&&is_array($re)&&!empty($re['price'])){
			return 1;
		}*/
		return 0;
    }
	
	//生成订单
	function booking($hotelid=0,$lastarrtime=18,$mobile='18581850569',$contactor='',$roomid,$roomNumber,$orderid,$sdate,$edate){
		global $myconfigs;$psm=$post="";
		$params=array(
			'agentid'=>$myconfigs['agentid'],'hotelid'=>$hotelid,'lastarrtime'=>$lastarrtime,'mobile'=>$mobile,'gender'=>'M',
			'contactor'=>$contactor,'roominfo'=>$roomid.'||'.$roomNumber,'orderid'=>$orderid,'sdate'=>$sdate,'edate'=>$edate
		);
		ksort($params);
		foreach($params as $k=>$v){
			$psm.="&".$k."=".$v;
		}
		$psm=trim($psm,'&');
		$sign=md5($psm.$myconfigs['key']);
		$paramsA=array(
			'agentid'=>$myconfigs['agentid'],'hotelid'=>$hotelid,'lastarrtime'=>$lastarrtime,'mobile'=>$mobile,'gender'=>'M','sign'=>$sign,
			'contactor'=>$contactor,'roominfo'=>$roomid.'||'.$roomNumber,'orderid'=>$orderid,'sdate'=>$sdate,'edate'=>$edate
		);
		ksort($paramsA);
		foreach($paramsA as $k=>$v){
			$post.="&".$k."=".$v;
		}
		$post=trim($post,'&');
		$re=$this->request_by_other("http://open.qmango.com/hotel/booking",$post);
		if($re&&is_array($re)){return array('orderId'=>$re['orderId'],'price'=>$re['price'],'commission'=>$re['commission']);}
		else{return 0;}
		//var_dump($re);echo $psm."/r/n".$sign;exit;
	}
	function request_by_other($remote_server,$post_string){
		error_reporting(0);
		$context = array(
			'http'=>array(
				'method'=>'POST',
				'header'=>'Content-type: application/x-www-form-urlencoded'."\r\n".'User-Agent : test'."\r\n".'Content-length: '.strlen($post_string)+8,
				'content'=>$post_string,
				'timeout'=>13
			)
		);
		$stream_context = stream_context_create($context);
		$data = file_get_contents($remote_server,FALSE,$stream_context);
		
		$logs="调用地址：".$remote_server."\r\n接收数据：".$data."\r\n发送数据：".$post_string;
        $this->setLogs($logs);
		
		$data = simplexml_load_string($data);
        $xml = @json_decode(@json_encode($data),TRUE);
        if($xml){
            if(isset($xml['status'])&&$xml['status']==1){return $xml;}
            return "数据错误，请联系技术人员！";
        }
        return "获取内容失败！";
	}
	
	//查询订单
	function selecteOrder(){//$orderId=''
		$orderId='7417523';
		global $myconfigs;$psm=$post="";
		$params=array('agentid'=>$myconfigs['agentid'],'orderid'=>$orderId);
		ksort($params);
		foreach($params as $k=>$v){
			$psm.="&".$k."=".$v;
		}
		$psm=trim($psm,'&');
		$sign=md5($psm.$myconfigs['key']);
		$paramsA=array('agentid'=>$myconfigs['agentid'],'orderid'=>$orderId,'sign'=>$sign);
		ksort($paramsA);
		foreach($paramsA as $k=>$v){
			$post.="&".$k."=".$v;
		}
		$post=trim($post,'&');
		$apiurl = "http://open.qmango.com/hotel/orders?".$post;
		$data = mb_convert_encoding(file_get_contents($apiurl), 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );
		
		$logs="调用地址：".$apiurl."\r\n接收数据：".$data."\r\n发送数据：".$post;
        $this->setLogs($logs);
		
		$data = simplexml_load_string($data);
        $xml = @json_decode(@json_encode($data),TRUE);
        if($xml){
            if(isset($xml['status'])&&$xml['status']==1){return $xml;}
            return "数据错误，请联系技术人员！";
        }
        return "获取内容失败！";
		echo "<pre>";var_dump($data);exit;
	}
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//获取房态
	function roombatch($hotelId='',$typeid='',$statime='',$endtime=''){
		$re=$this->curl('roombatch',array('hotelid'=>$hotelId,'roomId'=>$typeid,'sdate'=>$statime,'edate'=>$endtime));
		return $re;
	}
	
	//获取酒店列表
	function GetImmediateHotelListTO(){
		$re=$this->curl('GetImmediateHotelList');
		$string="";
		foreach($re['hotel'] as $k=>$v){
			$string.=",".$v['@attributes']['id'];
		}
		$string=trim($string,',');
		$string=explode(',',$string);
		return $string;
	}
}